import os
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.responses import RedirectResponse
from app import app as api_app
from config import APP_HOST, APP_PORT, DEBUG, BASE_DIR, UPLOADS_DIR

# 创建静态文件目录
static_dir = BASE_DIR / "static"
if not static_dir.exists():
    static_dir.mkdir(exist_ok=True, parents=True)

# 将API应用挂载到主应用
main_app = FastAPI(title="网络实验AI助手", description="基于通义千问API的网络实验PDF处理和问答系统")

# 将API应用路由直接添加到主应用
# for route in api_app.routes:
#     main_app.routes.append(route)

# 挂载API (保留这个挂载点以兼容旧的API路径)
main_app.mount("/api", api_app)

# 挂载静态文件
main_app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")

# 挂载上传目录为静态文件，直接提供图片访问
main_app.mount("/uploads", StaticFiles(directory=str(UPLOADS_DIR)), name="uploads")

@main_app.get("/")
async def root():
    """重定向到静态HTML页面"""
    return RedirectResponse(url="/static/index.html")

if __name__ == "__main__":
    uvicorn.run("main:main_app", host=APP_HOST, port=APP_PORT, reload=DEBUG)
